package com.llb666.e.shop.infrastructure.persistence;

import com.llb666.e.shop.api.vo.CategoryVo;
import com.llb666.e.shop.api.vo.NewItemsVo;
import com.llb666.e.shop.domain.entity.Category;
import com.llb666.e.shop.domain.repository.CategoryRepository;
import com.llb666.e.shop.infrastructure.convert.CategoryCvt;
import com.llb666.e.shop.infrastructure.mapper.CategoryMapper;
import com.llb666.e.shop.infrastructure.po.CategoryPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 斌~
 * @date 2022/8/29 下午9:45
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CategoryRepositoryImpl implements CategoryRepository {

    private final CategoryMapper categoryMapper;

    /**
     * 查询最上级分类列表.
     * @return 商品分类列表
     */
    @Override
    public List<Category> queryAllCategoryRoot() {
        Example example = new Example(CategoryPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type",1);
        return CategoryCvt.INSTANCE.po2doList(categoryMapper.selectByExample(example));
    }

    @Override
    public List<NewItemsVo> getSixNewItemsLazy(Integer rootCatId) {
        Map<String,Object> map = new HashMap<>(1);
        map.put("rootCatId",rootCatId);
        return categoryMapper.getSixNewItemsLazy(map);
    }

    @Override
    public List<CategoryVo> getSubCatList(Integer rootCatId) {
        return categoryMapper.getSubCatList(rootCatId);
    }

}
